using AdmissionQA.Application.Dtos.QAHistory;
using AdmissionQA.Application.ServiceInterfaces;
using Microsoft.AspNetCore.Mvc;

namespace AdmissionQA.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class QAHistoriesController(IQAHistoryServices qaHistoryServices) : ControllerBase
{
    private readonly IQAHistoryServices _qaHistoryServices = qaHistoryServices;

    [HttpGet("all")]
    public async Task<IActionResult> GetAll()
    {
        var result = await _qaHistoryServices.GetAllQAHistoriesAsync();
        return Ok(result);
    }

    [HttpGet("paged/{pageIndex}/{pageSize}")]
    public async Task<IActionResult> GetPaged(int pageIndex = 0, int pageSize = 10)
    {
        var result = await _qaHistoryServices.GetPagedQAHistoriesAsync(pageIndex, pageSize);
        return Ok(result);
    }

    [HttpGet("count")]
    public async Task<IActionResult> GetCount()
    {
        var result = await _qaHistoryServices.GetQAHistoryCountAsync();
        return Ok(result);
    }

    [HttpGet("by-type/{queryType}")]
    public async Task<IActionResult> GetByType(string queryType)
    {
        var result = await _qaHistoryServices.GetQAHistoriesByTypeAsync(queryType);
        return Ok(result);
    }

    [HttpGet("by-confidence/{confidence}")]
    public async Task<IActionResult> GetByConfidence(string confidence)
    {
        var result = await _qaHistoryServices.GetQAHistoriesByConfidenceAsync(confidence);
        return Ok(result);
    }

    [HttpPost("one")]
    public async Task<IActionResult> Create(QAHistoryDto qaHistory)
    {
        var result = await _qaHistoryServices.CreateQAHistoryAsync(qaHistory);
        return Ok(result);
    }

    [HttpPost("many")]
    public async Task<IActionResult> CreateMany(QAHistoriesDto qaHistories)
    {
        var result = await _qaHistoryServices.CreateQAHistoriesAsync(qaHistories);
        return Ok(result);
    }

    [HttpDelete("one/{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        var result = await _qaHistoryServices.DeleteQAHistoryAsync(id);
        return Ok(result);
    }

    [HttpDelete("many")]
    public async Task<IActionResult> DeleteMany(IEnumerable<Guid> ids)
    {
        var result = await _qaHistoryServices.DeleteQAHistoriesAsync(ids);
        return Ok(result);
    }

    /// <summary>
    /// 获取热门查询
    /// </summary>
    /// <param name="topK">返回前K个热门查询，默认10个</param>
    /// <returns>热门查询列表</returns>
    [HttpGet("popular-queries")]
    public async Task<IActionResult> GetPopularQueries([FromQuery] int topK = 10)
    {
        var result = await _qaHistoryServices.GetPopularQueriesAsync(topK);
        return Ok(result);
    }

    /// <summary>
    /// 获取查询统计信息
    /// </summary>
    /// <returns>统计信息</returns>
    [HttpGet("statistics")]
    public async Task<IActionResult> GetStatistics()
    {
        var result = await _qaHistoryServices.GetQueryStatisticsAsync();
        return Ok(result);
    }

    /// <summary>
    /// 智能问答并记录历史
    /// </summary>
    /// <param name="query">用户查询</param>
    /// <param name="queryType">查询类型</param>
    /// <returns>智能回答</returns>
    [HttpGet("smart-qa-with-history")]
    public async Task<IActionResult> SmartQAWithHistory([FromQuery] string query, [FromQuery] string queryType = "常见问题")
    {
        if (string.IsNullOrWhiteSpace(query))
        {
            return BadRequest("查询文本不能为空");
        }

        // 这里可以根据queryType调用不同的服务
        // 暂时返回一个示例回答
        var answer = "这是一个智能回答示例。";
        var confidence = "高";

        // 记录问答历史
        var qaHistory = new QAHistoryDto
        {
            Query = query,
            Answer = answer,
            QueryType = queryType,
            Confidence = confidence,
            UserIP = HttpContext.Connection.RemoteIpAddress?.ToString(),
            UserAgent = HttpContext.Request.Headers["User-Agent"].ToString()
        };

        await _qaHistoryServices.CreateQAHistoryAsync(qaHistory);

        return Ok(new
        {
            Question = query,
            Answer = answer,
            QueryType = queryType,
            Confidence = confidence,
            Source = "智能问答系统",
            Timestamp = DateTime.UtcNow
        });
    }
} 